package com.crazymaker.springcloud.base.security.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.crazymaker.springcloud.common.constants.SessionConstants;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

@Slf4j
public class AuthUtils {

    /**
     * 生成jwt token
     * @param subject
     * @param salt
     * @return
     */
    public static String buildToken(String subject, String salt) {
        Algorithm algorithm = Algorithm.HMAC256(salt);

        long start = System.currentTimeMillis() - 60000;
        Date end = new Date(start + SessionConstants.SESSION_TIME_OUT * 1000);
        return JWT.create()
                .withSubject(subject)
                .withIssuedAt(new Date(start))
                .withExpiresAt(end)
                .sign(algorithm);
    }

}
